#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N=2e5+5;

int n,m,K,raw[N],a[N],qc[N];
int d[20][N],X[4];
int mx[N][20],mn[N][20];
ll ans=0;

template<typename T>
    inline T max_eq(T &x,T y){
        return x= x>y? x:y;
    }

template<typename T>
    inline T min_eq(T &x,T y){
        return x= x<y? x:y;
    }

inline void add1(int x){
    for(int k=0;k<=K&&x-(1<<k)>=0;++k)
        max_eq(mx[x-(1<<k)+1][k],a[x]),
        min_eq(mn[x-(1<<k)+1][k],a[x]);
    int pos=x;
    bool win=true;
    for(int i=1;i<=K;++i){
        int side=pos&1;
        pos=(pos+1)/2;
        bool pass=false;
        int ls=(pos-1)*(1<<i)+1,rs=ls+(1<<(i-1));
        if(d[i][pos]==side) pass=(a[x]>=i);
        else if(d[i][pos]==0&&side==1) pass=(mn[ls][i-1]<i);
        else if(d[i][pos]==1&&side==0) pass=(mn[rs][i-1]<i);
        if(!pass){
            win=false;
            break;
        }
    }
    if(win) ans+=x;
}

inline void add2(int x){
    for(int k=0;k<=K&&x-(1<<k)>=0;++k)
        min_eq(mn[x-(1<<k)+1][k],0);
    int pos=x;
    bool win=true;
    for(int i=1;i<=K;++i){
        int side=pos&1;
        pos=(pos+1)/2;
        bool pass=false;
        int ls=(pos-1)*(1<<i)+1,rs=ls+(1<<(i-1));
        if(d[i][pos]==0&&side==1) pass=(mn[ls][i-1]<i);
        else if(d[i][pos]==1&&side==0) pass=(mn[rs][i-1]<i);
        else pass=true;
        if(!pass){
            win=false;
            break;
        }
    }
    if(win) ans+=x;
}

inline void add(int x){
    return x<=n? add1(x):add2(x);
}

inline void solve(){
    memset(mx,0,sizeof(mx));
    memset(mn,0x3f,sizeof(mn));
    ans=0;
    ll res=0;
    for(int i=0,j=1;j<=m;++j){
        while(i<(1<<K)&&i+1<=qc[j]) add(++i);
        res=res^(j*ans);
    }
    printf("%lld\n",res);
}

int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i) scanf("%d",&raw[i]);
    for(int i=1;i<=m;++i) scanf("%d",&qc[i]);
    sort(qc+1,qc+m+1);
    int pw=1; K=0;
    while(pw<n) pw<<=1, K++;
    for(int i=1;i<=K;++i){
        pw>>=1;
        for(int j=1;j<=pw;++j)
            scanf("%1d",&d[i][j]);
    }
    int T;
    scanf("%d",&T);
    while(T--){
        for(int i=0;i<=3;++i) scanf("%d",&X[i]);
        for(int i=1;i<=n;++i) a[i]=raw[i]^X[i%4];
        solve();
    }
    return 0;
}
